﻿#include "DataHeatGraph2DSettings.h"


namespace pst
{
    DataHeatGraph2DSettings::DataHeatGraph2DSettings(QObject* parent)
        : QObject(parent)
        , m_colorBar(new Style2D_ColorBar)
        , m_xAxisDisplay(new Style2D_Axis)
        , m_xAxisRange(new Style2D_AxisRange)
        , m_yAxisDisplay(new Style2D_Axis)
        , m_yAxisRange(new Style2D_AxisRange)
        , m_grid(new Style2D_GridLine)
        , m_title(new Style2D_Title)
    {
        m_yAxisDisplay->m_titleText = "y轴";
    }

    DataHeatGraph2DSettings::~DataHeatGraph2DSettings()
    {
        if (m_colorBar)
        {
            delete m_colorBar;
            m_colorBar = nullptr;
        }
        if (m_xAxisDisplay)
        {
            delete m_xAxisDisplay;
            m_xAxisDisplay = nullptr;
        }
        if (m_xAxisRange)
        {
            delete m_xAxisRange;
            m_xAxisRange = nullptr;
        }
        if (m_yAxisDisplay)
        {
            delete m_yAxisDisplay;
            m_yAxisDisplay = nullptr;
        }
        if (m_yAxisRange)
        {
            delete m_yAxisRange;
            m_yAxisRange = nullptr;
        }
        if (m_grid)
        {
            delete m_grid;
            m_grid = nullptr;
        }
        if (m_title)
        {
            delete m_title;
            m_title = nullptr;
        }
    }


    void DataHeatGraph2DSettings::setColorBar(Style2D_ColorBar* data)
    {
        m_colorBar = data;
    }

    Style2D_ColorBar* DataHeatGraph2DSettings::getColorBar() const
    {
        return m_colorBar;
    }

    void DataHeatGraph2DSettings::setXAxisDisplay(Style2D_Axis* data)
    {
        m_xAxisDisplay = data;
    }

    Style2D_Axis* DataHeatGraph2DSettings::getXAxisDisplay() const
    {
        return m_xAxisDisplay;
    }

    void DataHeatGraph2DSettings::setXAxisRange(Style2D_AxisRange* data)
    {
        m_xAxisRange = data;
    }

    Style2D_AxisRange* DataHeatGraph2DSettings::getXAxisRange() const
    {
        return m_xAxisRange;
    }

    void DataHeatGraph2DSettings::setYAxisDisplay(Style2D_Axis* data)
    {
        m_yAxisDisplay = data;
    }

    Style2D_Axis* DataHeatGraph2DSettings::getYAxisDisplay() const
    {
        return m_yAxisDisplay;
    }

    void DataHeatGraph2DSettings::setYAxisRange(Style2D_AxisRange* data)
    {
        m_yAxisRange = data;
    }

    Style2D_AxisRange* DataHeatGraph2DSettings::getYAxisRange() const
    {
        return m_yAxisRange;
    }

    void DataHeatGraph2DSettings::setGrid(Style2D_GridLine* data)
    {
        m_grid = data;
    }
    Style2D_GridLine* DataHeatGraph2DSettings::getGrid() const
    {
        return m_grid;
    }

    void DataHeatGraph2DSettings::setTitle(Style2D_Title* data)
    {
        m_title = data;
    }

    Style2D_Title* DataHeatGraph2DSettings::getTitle() const
    {
        return m_title;
    }

    //void DataHeatGraph2DSettings::copyData(const DataHeatGraph2DSettings* data)
    //{
    //    if (this == data || data == nullptr) return;
    //    this->m_colorBar->copyData(data->m_colorBar);
    //    this->m_xAxisDisplay->copyData(data->m_xAxisDisplay);
    //    this->m_xAxisRange->copyData(data->m_xAxisRange);
    //    this->m_yAxisDisplay->copyData(data->m_yAxisDisplay);
    //    this->m_yAxisRange->copyData(data->m_yAxisRange);
    //    this->m_grid->copyData(data->m_grid);
    //    this->m_title->copyData(data->m_title);
    //}

    QDomElement& DataHeatGraph2DSettings::writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso)
    {
        QDomElement _DataHeatGraph2DSettingsDataRoot = doc->createElement("DataHeatGraph2DSettings");
        QDomAttr idattr = doc->createAttribute("ID");
        idattr.setValue(QString::number(1));
        _DataHeatGraph2DSettingsDataRoot.setAttributeNode(idattr);

        m_colorBar->writeToProjectFile(doc, &_DataHeatGraph2DSettingsDataRoot, isdiso);
        m_xAxisDisplay->writeToProjectFile(doc, &_DataHeatGraph2DSettingsDataRoot, isdiso);
        m_xAxisRange->writeToProjectFile(doc, &_DataHeatGraph2DSettingsDataRoot, isdiso);
        m_yAxisDisplay->writeToProjectFile(doc, &_DataHeatGraph2DSettingsDataRoot, isdiso);
        m_yAxisRange->writeToProjectFile(doc, &_DataHeatGraph2DSettingsDataRoot, isdiso);
        m_grid->writeToProjectFile(doc, &_DataHeatGraph2DSettingsDataRoot, isdiso);
        m_title->writeToProjectFile(doc, &_DataHeatGraph2DSettingsDataRoot, isdiso);

        element->appendChild(_DataHeatGraph2DSettingsDataRoot);

        return _DataHeatGraph2DSettingsDataRoot;
    }

    void DataHeatGraph2DSettings::readFromProjectFile(QDomElement* element, bool isdiso)
    {
        int id = element->attribute("ID").toInt();

        auto _ColorBarEle = element->elementsByTagName("Style2D_ColorBar");
        auto _AxisDisplayEle = element->elementsByTagName("Style2D_Axis");
        int displayCount = _AxisDisplayEle.count(); // 值为2
        auto _AxisRangeEle = element->elementsByTagName("Style2D_AxisRange");
        int rangeCount = _AxisRangeEle.count(); // 值为2
        auto _gridEle = element->elementsByTagName("Style2D_GridLine");
        auto _titleEle = element->elementsByTagName("Style2D_Title");
        auto _legendEle = element->elementsByTagName("Style2D_Legend");

        m_colorBar->readFromProjectFile(&_ColorBarEle.at(0).toElement(), isdiso);
        m_xAxisDisplay->readFromProjectFile(&_AxisDisplayEle.at(0).toElement(), isdiso);
        m_xAxisRange->readFromProjectFile(&_AxisRangeEle.at(0).toElement(), isdiso);
        m_yAxisDisplay->readFromProjectFile(&_AxisDisplayEle.at(1).toElement().toElement(), isdiso);
        m_yAxisRange->readFromProjectFile(&_AxisRangeEle.at(1).toElement(), isdiso);
        m_grid->readFromProjectFile(&_gridEle.at(0).toElement(), isdiso);
        m_title->readFromProjectFile(&_titleEle.at(0).toElement(), isdiso);
    }


}
